package org.osoftware.mumi.dao.interceptor;

import java.util.Date;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.osoftware.mumi.dao.model.BaseObject;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class BaseDaoInterceptor {

	private static final String PACKAGE_TO_LISTEN = "org.osoftware.mumi.dao.hibernate";
	private static final String LOG_SAVE_METHOD = "Saving: ";
	
	
	Logger LOG;

	@Before("execution(* " + PACKAGE_TO_LISTEN + ".*.save(..))")
	public void logBefore(JoinPoint joinPoint) {
		LOG = LOG.getLogger(joinPoint.getSignature().getDeclaringType());
		
		BaseObject entity = null;
		if (joinPoint.getArgs() != null && joinPoint.getArgs().length > 0) {
			entity = (BaseObject)joinPoint.getArgs()[0];
			LOG.debug(LOG_SAVE_METHOD + entity);
			entity.setCreationDate(new Date());
		}
	}
	
}
